#include "Player.h"

Player::Player(int x, int y, SpriteAnimation* animation) {
	this->xPos = x;
	this->yPos = y;
	this->prevXPos = x;
	this->prevYPos = y;
	this->animation = animation;
	this->clip.h = 64;
	this->clip.w = 64;
	this->clip.x = 0;
	this->clip.y = 0;
	this->dir = Renderable::RIGHT;
	this->isMoving = false;
	this->isJumping = false;
	speed = 1000;
	currJumpVelocity = 0;
	startingJumpVelocity = 500;
}

Player::~Player() {

}

void Player::setAnimation(SpriteAnimation* animation) {
	this->animation = animation;
}

void Player::jump() {
	if (isJumping == false) {
		isJumping = true;
		currJumpVelocity = startingJumpVelocity;
	}
}

//Entity members
void Player::setXPos(double x) {
	this->xPos = x;
	this->isMoving = false;
	this->isJumping = false;
}

void Player::setYPos(double y) {
	this->yPos = y;
	this->isMoving = false;
	this->isJumping = false;
}

void Player::setName(std::string name) {
	this->name = name;
}

//Renderable members
void Player::setTexture(SDL_Surface* texture) {
	this->animation->setTexture(texture);
}

void Player::setClip(SDL_Rect clip) {
	this->clip = clip;
}

void Player::update(Uint32 delta) {
	animation->update(delta);
	if (isMoving) {
		prevXPos = xPos;
		prevYPos = yPos;
		switch (dir) {
			case Renderable::RIGHT: xPos += speed * (delta / 1000.0f); break;
			case Renderable::LEFT: xPos -= ((double)speed / 1000.0) * delta; break;
		}
	}
	isMoving = false;
	if (isJumping) {
		yPos -= ((double)currJumpVelocity / 1000.0) * delta;
		currJumpVelocity -= Entity::GRAVITY;
	}
}

void Player::setDirection(Direction dir) {
	this->dir = dir;
	switch (dir) {
		case Renderable::RIGHT: animation->setAnimation(0); break;
		case Renderable::LEFT: animation->setAnimation(1); break;
		case Renderable::TOP: animation->setAnimation(2); break;
		case Renderable::BOTTOM: animation->setAnimation(3); break;
	}
	isMoving = true;
}

void Player::handleCollision(Direction dir) {
	if (dir == Renderable::TOP || dir == Renderable::BOTTOM) {
		yPos = prevYPos;
		if (dir == Renderable::BOTTOM) {
			isJumping = false;
		}
	}
	else if (dir == Renderable::LEFT || dir == Renderable::RIGHT) {
		xPos = prevXPos;
	}
}